package com.ygqh.baby.controller.mobile;

import java.util.Arrays;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.CheckStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.UserType;
import com.ygqh.baby.po.YgAgent;
import com.ygqh.baby.po.YgAgentInviteRecord;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgAgentInviteRecordService;
import com.ygqh.baby.service.YgAgentService;
import com.ygqh.baby.service.impl.DocumentConstant;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.SessionUtil;

/**
 * 代理用户
 * 
 * 
 *
 */
@Controller
@RequestMapping("/mobile/agent")
public class YgAgentController {
	protected final Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private YgAgentService ygAgentService;
	@Autowired
	private DocumentConstant documentConstant;
	@Autowired
	private YgAgentInviteRecordService ygAgentInviteRecordService;

	/**
	 * 保存代理申请
	 * 
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "saveAgent", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject saveAgent(Long id, String trueName, String identityCard, String email, String address, String childAge, String fromChannel,
			String advantage, String suggest, String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		YgAgent dbAgent = ygAgentService.findByRecent(user.getId(), UserType.UserAgent);
		if (dbAgent != null && !dbAgent.getCheckStatus().equals(CheckStatus.Reject)) {
			return new JSONPObject(callback, Message.error(documentConstant.SEND_COUNT_OVER, null));
		}
		YgAgent agent = new YgAgent();
		agent.setUserId(user.getId());
		/* agent.setId(id); */
		agent.setAddress(address);
		agent.setTrueName(trueName);
		agent.setTelPhone(user.getUserName());
		agent.setChildAge(childAge);
		agent.setEmail(email);
		agent.setFromChannel(fromChannel);
		agent.setIdentityCard(identityCard);
		agent.setAdvantage(advantage);
		agent.setCreateTime(new Date());
		agent.setSuggest(suggest);
		agent.setCheckBy(null);
		agent.setCheckTime(null);
		agent.setCheckStatus(CheckStatus.Waiting);
		ygAgentService.saveOrUpdate(agent);

		JSONPObject json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, null));
		return json;
	}

	/**
	 * 根据用户获得代理信息
	 * 
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "findAgent", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject findAgent(String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		YgAgentInviteRecord agent = ygAgentInviteRecordService.findValidByReceiveUserId(user.getId());
		JSONPObject json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, agent));
		return json;
	}

	/**
	 * 验证用户是否有请求代理资格
	 * 
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "validateAgent", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject validateAgent(String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		boolean result = ygAgentService.validateAgentStatus(user.getId());
		JSONPObject json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, result));
		return json;
	}

	/**
	 * 填写邀请码成为代理
	 * 
	 * @param inviteCode
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "rechargeUserAgent", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject rechargeUserAgent(@RequestParam String inviteCode, String callback) {
		return new JSONPObject(callback, ygAgentInviteRecordService.rechargeUserAgent(inviteCode, SessionUtil.getCurrentUser().getId()));
	}

	/**
	 * 获取代理邀请码信息
	 * 
	 * @param isReceive
	 *            是否已被领取
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "findInviteRecordByIsReceive", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject findInviteRecordByIsReceive(Boolean isReceive, String callback) {
		Message message = null;
		Long userId = SessionUtil.getCurrentUser().getId();
		if (isReceive != null && isReceive) {
			message = Message.success(ygAgentInviteRecordService.findReceiveListByUserId(userId));
		} else {
			Date now = new Date();
			Date startDate = DateConvertUtils.getMonthStartDate(now);
			Date endDate = DateConvertUtils.getDateEnd(DateConvertUtils.getMonthEndDate(now));
			message = Message.success(ygAgentInviteRecordService.findByUserIdListAndBetweenTime(Arrays.asList(userId), startDate, endDate));
		}
		return new JSONPObject(callback, message);
	}

	/**
	 * 立即成为新代理 （只适用于原女王、原代理和待审核的代理）
	 * 
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "rechargeUserAgentRightNow", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject rechargeUserAgentRightNow(String callback) {
		return new JSONPObject(callback, ygAgentInviteRecordService.rechargeUserAgentRightNow(SessionUtil.getCurrentUser().getId(), Boolean.FALSE));
	}

	/**
	 * 邀请用户立即成为新代理 （只适用于普通用户）
	 * 
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "rechargeUserAgentByInviteUser", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject rechargeUserAgentByInviteUser(String callback) {
		return new JSONPObject(callback, ygAgentInviteRecordService.rechargeUserAgentRightNow(SessionUtil.getCurrentUser().getId(), Boolean.TRUE));
	}
	
	@RequestMapping(value = "applyToAgent", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject applyToAgent(String callback) {
		return new JSONPObject(callback, ygAgentInviteRecordService.applyToAgent(SessionUtil.getCurrentUser().getId()));
	}

}
